package com.ntschy.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ntschy.common.constant.Constants;
import com.ntschy.common.core.entity.dataobject.SysDictData;
import com.ntschy.common.core.redis.RedisCache;
import com.ntschy.common.utils.spring.SpringUtils;

import java.util.Collection;
import java.util.List;

/**
 * 字典工具类
 * @author ezreal_geng
 */
public class DictUtils {

    /**
     * 设置字典缓存
     *
     * @param key       参数键
     * @param dictDataList 字典数据列表
     */
    public static void setDictCache(String key, List<SysDictData> dictDataList) {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), dictDataList);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     */
    public static List<SysDictData> getDictCache(String key) {
        List<SysDictData> cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
        if (ObjectUtil.isNotNull(cacheObj)) {
            return cacheObj;
        }
        return null;
    }

    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType  字典类型
     * @param dictValue 字典值
     * @param separator 分隔符
     * @return 字典标签
     */
    public static String getDictLabel(String dictType, String dictValue) {
        StringBuilder str = new StringBuilder();
        List<SysDictData> dataList = getDictCache(dictType);

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (SysDictData dict : dataList) {
                if (dictValue.equals(dict.getDictValue())) {
                    str.append(dict.getDictLabel());
                    break;
                }
            }
        }
        return str.toString();
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType  字典类型
     * @param dictLabel 字典标签
     * @return 字典值
     */
    public static String getDictValue(String dictType, String dictLabel) {
        StringBuilder str = new StringBuilder();
        List<SysDictData> dataList = getDictCache(dictType);

        if (CollectionUtil.isNotEmpty(dataList)) {
            for (SysDictData dict : dataList) {
                if (dictLabel.equals(dict.getDictLabel())) {
                    str.append(dict.getDictValue());
                    break;
                }
            }
        }
        return str.toString();
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return Constants.SYS_DICT_KEY + configKey;
    }

    /**
     * 清空字典缓存
     */
    public static void clearDictCacheAll() {
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(Constants.SYS_DICT_KEY + "*");
        SpringUtils.getBean(RedisCache.class).deleteObject(keys);
    }

    /**
     * 清空具体字典类型缓存
     */
    public static void clearDictCache(String dictType) {
        String key = Constants.SYS_DICT_KEY + dictType;
        SpringUtils.getBean(RedisCache.class).deleteObject(key);
    }
}
